<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\BridgeRequest;
use App\Models\Bridge;
use App\Models\BridgeAge;
use App\Models\BridgeDetail;
use App\Models\BridgeLevel;
use App\Models\BridgeType;
use Illuminate\Http\Request;


class BridgesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return Bridge::with('detail')->paginate();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $ages = BridgeAge::orderBy('sort')->get(['id', 'name']);
        $types = BridgeType::orderBy('sort')->get(['id', 'name']);
        $levels = BridgeLevel::orderBy('sort')->get(['id', 'name']);

        return compact('ages', 'types', 'levels');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(BridgeRequest $request)
    {
        // 上传文件
        if ($request->file('bridges')){
            return $this->upload($request->file('bridges'));
        }

        $data = $request->only('age_id', 'type_id', 'level_id', 'bridge_name', 'front_cover', 'longitude', 'latitude');
        $bridge = Bridge::create($data);
        $data = $request->only('bridge_name', 'couplet', 'adorn', 'introduction', 'journal', 'heritage_list', 'attractions', 'bridge_type', 'feature');
        $data['bridge_id'] = $bridge->id;
        BridgeDetail::create($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $bridge = Bridge::where('id', $id)->with('detail')->firstOrFail();
        $ages = BridgeAge::orderBy('sort')->get(['id', 'name']);
        $types = BridgeType::orderBy('sort')->get(['id', 'name']);
        $levels = BridgeLevel::orderBy('sort')->get(['id', 'name']);

        return compact('ages', 'types', 'levels', 'bridge');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(BridgeRequest $request, Bridge $bridge)
    {
        // 上传文件
        if ($request->file('bridges')){
            return $this->upload($request->file('bridges'));
        }

        $data = $request->only('age_id', 'type_id', 'level_id', 'bridge_name', 'front_cover', 'longitude', 'latitude');
        $bridge->update($data);
        $data = $request->only('bridge_name', 'couplet', 'adorn', 'introduction', 'journal', 'heritage_list', 'attractions', 'bridge_type', 'feature');
        $bridge->detail()->update($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Bridge $bridge)
    {
        $bridge->delete();
    }
}
